package com.atmilan.quartz.utils.product;

import com.alibaba.fastjson.JSONObject;
import com.atmilan.quartz.config.RestTemplateConfig;
import com.atmilan.quartz.model.dto.leam.LeamProductResDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 拉取商品源数据
 */

@Slf4j
@Service
public class LeamProductUtil {

    @Autowired
    private RestTemplateConfig restTemplateConfig;

    @Value("${leam.url}")
    private String leamUrl;
    @Value("${leam.token}")
    private String leamToken;

    /**
     * 获取图片前缀url
     */
    public String selectImageUrl() {
        //组装请求url
        JSONObject message = null;
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(leamUrl)
                    .append("/ApiV3/token/")
                    .append(leamToken);
            // 发送请求
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            ResponseEntity<String> response = restTemplate.getForEntity(sb.toString(), String.class);
            if (HttpStatus.OK != response.getStatusCode()) {
                message = JSONObject.parseObject(response.getBody());
                String errorMessage = message.getString("error");

                log.info("--------------- leam获取图片前缀url异常{} ---------------", errorMessage);
            }
            message = JSONObject.parseObject(response.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message.getString("image_url");
    }

    /**
     * 获取产品列表
     * @return
     */
    public LeamProductResDTO selectLeam() throws Exception {
        //组装请求url
        LeamProductResDTO leamProductResDTO = null;
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(leamUrl)
                    .append("/ApiV3/token/")
                    .append(leamToken)
                    .append("/callType/allStockGroup");
            // 发送请求
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            ResponseEntity<String> response = restTemplate.getForEntity(sb.toString(), String.class);
            if (HttpStatus.OK != response.getStatusCode()) {
                JSONObject message = JSONObject.parseObject(response.getBody());
                String errorMessage = message.getString("error");

                log.info("--------------- leam获取产品列表异常{} ---------------", errorMessage);
            }
            ObjectMapper mapper = new ObjectMapper();
            leamProductResDTO = mapper.readValue(response.getBody(), LeamProductResDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return leamProductResDTO;
    }
}
